<template>
    <div class="add-address">
        <mt-field placeholder="收货人" v-model="username" class="person-cell"></mt-field>
        <mt-field placeholder="手机号码" type="tel" v-model="phone" class="person-cell"></mt-field>
        <mt-field placeholder="所在城市" v-model="city" class="person-cell"></mt-field>
        <mt-field placeholder="详细地址：街道、门牌号码等" type="textarea" rows="4" v-model="introduction" class="person-cell"></mt-field>
        <mt-cell title="设置默认地址" style="margin-top:.2rem;">
             <mt-switch v-model="value" @change="changeValue"></mt-switch>
        </mt-cell>
         <mt-cell title="删除收货地址" style="margin-top:.2rem;color:#ff2d41;" @click.native="saveAddress(1)"></mt-cell>
        <div class="save-address" @click="saveAddress(0)">保存</div>
    </div>
</template>
<script>
import { post } from '../../../../util/http.js'
import { Toast } from 'mint-ui'
export default {
    data() {
        return {
            addressItem: [],
            username: '',
            phone: '',
            city: '',
            introduction: '',
            default: 1,
            value: false
        }
    },
     mounted() {
        this.$nextTick(() => {
            let info = JSON.parse(window.localStorage.getItem('dongHang_data'))
            post('http://dlsapi.rz158.com/address/selectAddress', {
                agentId: info.agentId
            }).then(res => {
                if(res.code != 0) {
                    Toast({
                        message: res.message,
                        duration: 1000
                    })
                } else {
                   this.addressItem =  res.t.filter(item => {
                        return item.id == this.$route.params.id
                   })
                   this.username = this.addressItem[0].name
                   this.phone = this.addressItem[0].telephone
                   this.city = this.addressItem[0].city
                   this.introduction = this.addressItem[0].detailAddress
                   if ( this.addressItem[0].isDefault) {
                       this.value = true
                   } else {
                       this.value = false
                   }
                   this.default = this.addressItem[0].isDefault
                }
            })
        })
    },
    methods: {
        checkPhone (val) {
            let isPhone = /^(13|14|15|16|17|18|19)[0-9]{9}$/.test(val) ? 1 : 0
            if (isPhone === 1) {
            return {
                stat: 1
             }
            }
        },
        changeValue() {
            if (this.value) {
                this.default = 1
            } else {
                this.default = 0
            }
        },
        saveAddress(type) {
            let info = JSON.parse(window.localStorage.getItem('dongHang_data'))
            if( this.username!='' && this.phone!= ''&& this.city!= '' && this.introduction!= '') {
            let res = this.checkPhone(this.phone)
                if(res) {
                    post('http://dlsapi.rz158.com/address/dealReceiveAddress', {
                        agentId: info.agentId,
                        name: this.username,
                        telephone: this.phone,
                        city: this.city,
                        detailAddress: this.introduction,
                        isDefault: this.default,
                        type: type,
                        id: parseInt(this.$route.params.id)
                    }).then(res => {
                        if(res.code == 0) {
                            Toast({
                                message: res.message,
                                duration: 1000
                            })
                            this.$router.push('/my_address')   //操作成功
                        } else {
                            Toast({
                                message: res.message,
                                duration: 1000
                            })
                        }
                    })
                } else {
                     Toast({
                        message: '请输入正确手机号',
                        duration: 1000
                    })
                }
            } else {
                Toast({
                    message: '请将信息填写完整',
                    duration: 1000
                })
            } 
      }
    }
}
</script>

<style lang="less" scoped>
.add-address{
    background: #efefef;
    height: 13.34rem;
}
.save-address{
    position: absolute;
    font-size: .32rem;
    color: #ffffff;
    background: #29abae;
    height: .82rem;
    width: 100%;
    z-index: 99;
    bottom: 0rem;
    text-align: center;
    line-height: .82rem;
}
 .person-cell {
    border-bottom: 1px solid #cccccc;
}
</style>

